import ch.rolandschaer.ascrblr.util.ServiceException;

import java.io.*;
import java.awt.image.BufferedImage;

import java.util.logging.Level;
import java.util.logging.Logger;
import java.net.URI;

/**
 *
 * @author Dark_Wolf
 */
public abstract class Avatar  {
    
    
    
    protected BufferedImage image = null;
    protected String artist;
    protected String album;
    protected URI folder = null;
    protected boolean gotImage;
    protected boolean loaded = false;
    protected String fileURI;
    
    protected CDShelf shelf;
    protected CrystalCaseFactory fx;
    protected AlbumDownloader albumDownloader = null;
    
    
  
    
    public void downLoadImage() {
        if (!gotImage) {
            forceDownloadImage();
        }
    }
    
    public void forceDownloadImage(){
        try {
            if (albumDownloader == null) {

                albumDownloader = new AlbumDownloader(album, artist);

            }
            if (albumDownloader.saveArt(fileURI)) {

                folder = AlbumHelper.songbirdToJavaPath(fileURI, artist + " - " + album,"jpg");
                if (shelf.getWriteMp3()) {

                    embedImage();



                }
            }
            gotImage = true;
        } catch (ServiceException ex) {
            Logger.getLogger(Avatar.class.getName()).log(Level.SEVERE, null, ex);
        }     
            
       
    }
    
    
   public abstract void embedImage();
    
   public abstract void setImage(File newFile);
    
    public abstract boolean loadImage();
    
    public void unloadImage() {
        if (loaded) {
            image = null;
            loaded = false;
        }
    }
    
    public BufferedImage getImage() {
        
        if(!loaded)
            loadImage();
        
        if (image == null)
            System.out.println("The image is lost! Loooooost! (No really, someone replaced your RAM with smoked Salmon)");
        
        return image;
        
                        /*
                        try {
                         
                                        return fx.createReflectedPicture(fx.createCrystalCase(ImageIO.read(file)));
                         
                                }
                                catch(IOException e)
                                {
                                        System.out.println(e);
                                        System.out.println("loadImageproblem: " + file);
                                        e.printStackTrace();
                                }
                                return null;
                         */
        
    }
    
    public abstract BufferedImage getRawImage();
    
    public String getText() {
        return artist + " - " + album;
    }
    
    public String getArtist()
    {
        return artist;
    }
    
    public String getAlbum()
    {
        return album;
    }
    
    @Override
    public boolean equals(Object o)
    {
        if (o instanceof Avatar)
        {
            Avatar a =  (Avatar)o;
            return (artist.equals(a.artist) && album.equals(a.album));
        }
        return false;
    }
    @Override
    public int hashCode()
    {
        return artist.hashCode()+album.hashCode();
    }
}



